﻿/*
 * This file is part of ActivityInfo.
 *
 * ActivityInfo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ActivityInfo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ActivityInfo.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2009 Alex Bertram and contributors.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.SystemUI;


namespace ActivityInfo.ArcGIS.Tiles
{
    [Guid("810469FE-F8E3-4F0C-80AA-FB1749E30626")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("ArcTiles.ZoomCommand")]
    public class ZoomCommand : BaseCommand, IToolControl
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Register(regKey);
            ControlsCommands.Register(regKey);
        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Unregister(regKey);
            ControlsCommands.Unregister(regKey);
        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper = null;
        private ZoomControl m_control = null;
        private IActiveViewEvents_ViewRefreshedEventHandler m_viewChangedHandler;

        public ZoomCommand()
        {
            // Only one instance of a ToolControl can exist within 
            // an application framework at any given time. This is 
            // because the hWnd property is passed as window handle to the 
            // control. To prevent a user from dragging two instances of a 
            // ToolControl into an application framework set the 
            // ICommand::Category property to an empty string.
            // This will prevent the ToolControl from appearing in the 
            // customize dialog.
            // See: http://edndoc.esri.com/arcobjects/9.2/ComponentHelp/esriSystemUI/IToolControl.htm
            base.m_category = ""; 
         
            
            base.m_caption = "Zoom";
            base.m_message = "Zooms to a fixed zoom level (0 - 16)";
            base.m_toolTip = "Zooms to a fixed zoom level (0 - 16)";
            base.m_name = "ArcTiles_ZoomCommand";

           // IActiveViewEvents events = (IActiveViewEvents)m_hookHelper.ActiveView;
           // m_viewChangedHandler = new IActiveViewEvents_ViewRefreshedEventHandler(onViewRefreshed);
           //// events.ViewRefreshed += m_viewChangedHandler;

            //try
            //{
            //    string bitmapResourceName = GetType().Name + ".bmp";
            //    base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
            //}
            //catch (Exception ex)
            //{
            //    System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            //}
        }

        private void onViewRefreshed(IActiveView activeView, esriViewDrawPhase drawPhase,
            object obj, ESRI.ArcGIS.Geometry.IEnvelope target)
        {

        }

        #region Overriden Class Methods

        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
                return;

            try
            {
                m_hookHelper = new HookHelperClass();
                m_hookHelper.Hook = hook;
                if (m_hookHelper.ActiveView == null)
                    m_hookHelper = null;
            }
            catch
            {
                m_hookHelper = null;
            }

            if (m_hookHelper == null)
                base.m_enabled = false;
            else
                base.m_enabled = true;
        }

        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {

        }


        #endregion

        #region IToolControl Members

        public bool OnDrop(esriCmdBarType barType)
        {
            return false;
        }

        public void OnFocus(ICompletionNotify complete)
        {
            
        }

        public int hWnd
        {
            get
            {
                if (m_control == null)
                {
                    m_control = new ZoomControl(m_hookHelper);
                }
                return m_control.Handle.ToInt32();
            }
        }
        

        #endregion
    }
}
